package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.datadev.FlinkPointResponse;
import com.sh.data.engine.application.model.dto.datadev.FlinkTaskRecordResponse;
import com.sh.data.engine.application.model.request.datadev.RestartJobRequest;
import com.sh.data.engine.application.model.request.datadev.TaskRecordInfoRequest;
import com.sh.data.engine.application.service.datadev.FlinkRecordApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.datadev.flink.model.domain.FlinkLogDomain;
import com.sh.data.engine.domain.datadev.flink.model.domain.FlinkTaskSummaryDomain;
import com.sh.data.engine.domain.datadev.flink.service.FlinkTaskV1Service;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author : fuchen
 * @date : 2021/6/16 15:21
 */
@Validated
@RestController
@RequestMapping("/flink/record")
@Tag(name = "实时计算运维管理", description = "实时计算运维管理")
public class FlinkRecordController {

    @Autowired
    private FlinkTaskV1Service flinkTaskV1Service;

    @Autowired
    private FlinkRecordApplicationService flinkRecordApplicationService;


    @GetMapping("/running/list")
    @Operation(summary = "flink运行记录")
////    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<PageResult<FlinkTaskRecordResponse>> getTaskRecordPageInfo(
        TaskRecordInfoRequest taskRecordInfoRequest) {
        var result = flinkRecordApplicationService.getTaskRecordPageInfo(taskRecordInfoRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/task/detail")
    @Operation(summary = "任务详情")
////    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<FlinkTaskRecordResponse> getFlinkJobTaskDetailById(
        @Parameter(description = "运行记录ID", required = true) @RequestParam Long id) {
        var result = flinkRecordApplicationService.getFlinkJobTaskDetailById(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/running/detail")
    @Operation(summary = "日志详情")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<FlinkLogDomain> getTaskRecordDetailById(
        @Parameter(description = "记录id", required = true) @RequestParam Long id) {
        var result = flinkRecordApplicationService.getTaskRecordDetailById(id);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/running/stop")
    @Operation(summary = "停止任务")
    @LogExecution(operation = EngineOperation.STOP_TASK, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.STOP_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> stopFlinkTaskById(
        @Parameter(description = "记录id", required = true) @RequestParam Long id,
        @Parameter(description = "是否保存savepoint 0-不保存 1-保存", required = false, schema = @Schema(defaultValue = "1"))
        @RequestParam(required = false, defaultValue = "1")
        Integer status) {
        var result = flinkRecordApplicationService.stopFlinkTaskById(id, status);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/running/retry")
    @Operation(summary = "重试任务")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> retryJob(
        @Parameter(description = "记录id", required = true) @RequestParam Long id) {
        var result = flinkRecordApplicationService.retryJob(id);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/running/restart")
    @Operation(summary = "重跑任务")
    @LogExecution(operation = EngineOperation.START_TASK, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.START_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> restartJob(@Valid @RequestBody RestartJobRequest request) {
        var result = flinkRecordApplicationService.restartJob(request);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/running/continue")
    @Operation(summary = "续跑任务")
    @LogExecution(operation = EngineOperation.START_TASK, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.START_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> continueJob(
        @Parameter(description = "记录id", required = true) @RequestParam Long id,
        @Parameter(description = "point path", required = true) @RequestParam String point) {
        var result = flinkRecordApplicationService.continueJob(id, point);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/running/checkpoints")
    @Operation(summary = "获取checkpoints")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<List<FlinkPointResponse>> getCheckPointsById(
        @Parameter(description = "记录id", required = true) @RequestParam Long id) {
        var result = flinkRecordApplicationService.getCheckPointsById(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/running/getLastSavepoint")
    @Operation(summary = "获取最新的savepoints")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<FlinkPointResponse> getLastSavepoint(
        @Parameter(description = "记录id", required = true) @RequestParam Long id) {
        var result = flinkRecordApplicationService.getLastSavepoint(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/running/summary")
    @Operation(summary = "运行概览summary")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 0, isAdd = 1)
    public RestResponseEntity<FlinkTaskSummaryDomain> getSummary() {
        var result = flinkRecordApplicationService.getSummary();
        return RestResponseEntity.success(result);
    }


}
